package com.chessimo.main;

import java.awt.Container;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URI;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 * An extension of JLabel which looks like a link and responds appropriately
 * when clicked. Note that this class will only work with Swing 1.1.1 and later.
 * Note that because of the way this class is implemented, getText() will not
 * return correct values, user <code>getNormalText</code> instead.
 */

public class LinkLabel extends JLabel {

	public static void main(String[] args)
	{
	    JFrame frame = new JFrame("Linkify Test");
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setSize(400, 100);
	    frame.setLocationRelativeTo(null);
	    Container container = frame.getContentPane();
	    container.setLayout(new GridBagLayout());
	    container.add(new JLabel("Click "));
	    container.add(linkify("this", "http://facebook.com", "Facebook"));
	    container.add(new JLabel(" link to open Facebook."));
	    frame.setVisible(true);
	}
	
	public static JLabel linkify(final String text, String URL, String toolTip)
	{
	    URI temp = null;
	    try
	    {
	        temp = new URI(URL);
	    }
	    catch (Exception e)
	    {
	        e.printStackTrace();
	    }
	    final URI uri = temp;
	    final JLabel link = new JLabel();
	    link.setText("<HTML><FONT color=\"#000099\">"+text+"</FONT></HTML>");
	    if(!toolTip.equals(""))
	        link.setToolTipText(toolTip);
	    link.setCursor(new Cursor(Cursor.HAND_CURSOR));
	    link.addMouseListener(new MouseListener()
	    {
	        public void mouseExited(MouseEvent arg0)
	        {
	            link.setText("<HTML><FONT color=\"#000099\">"+text+"</FONT></HTML>");
	        }

	        public void mouseEntered(MouseEvent arg0)
	        {
	            link.setText("<HTML><FONT color=\"#000099\"><U>"+text+"</U></FONT></HTML>");
	        }

	        public void mouseClicked(MouseEvent arg0)
	        {
	            if (Desktop.isDesktopSupported())
	            {
	                try
	                {
	                    System.out.println("UHULLLLL");
	                }
	                catch (Exception e)
	                {
	                    e.printStackTrace();
	                }
	            }
	            else
	            {
	                JOptionPane pane = new JOptionPane("Could not open link.");
	                JDialog dialog = pane.createDialog(new JFrame(), "");
	                dialog.setVisible(true);
	            }
	        }

	        public void mousePressed(MouseEvent e)
	        {
	        }

	        public void mouseReleased(MouseEvent e)
	        {
	        }
	    });
	    return link;
	}	

}